package com.study.springbootsecurity.service.impl;

import com.aliyun.sdk.service.dysmsapi20170525.models.QuerySendDetailsResponse;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.study.springbootsecurity.config.properties.AliyunSmsProperties;
import com.study.springbootsecurity.domain.dto.query.SendStatisticsQueryDTO;
import com.study.springbootsecurity.entity.LoginUser;
import com.study.springbootsecurity.entity.Role;
import com.study.springbootsecurity.entity.SysUser;
import com.study.springbootsecurity.entity.business.SmsCodeQuery;
import com.study.springbootsecurity.exception.BusinessException;
import com.study.springbootsecurity.service.SmsCodeService;
import com.study.springbootsecurity.service.SysUserService;
import com.study.springbootsecurity.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO 手机验证码业务接口
 * @author: Huangjianyun
 * @date: 2025-06-13 16:56
 */
@Service
public class SmsCodeServiceImpl implements SmsCodeService {

    private static final String SMS_CODE_PREFIX="sms:code:";

    @Autowired
    RedisCache redisCache;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    AliyunSmsProperties  aliyunSmsProperties;

    @Autowired
    SmsClientFactory smsClientFactory;

    @Override
    public void sendSmsCode(SmsCodeQuery smsCodeQuery) {
        SmsUtil smsUtil = new SmsUtil(aliyunSmsProperties,smsClientFactory);
        SendSmsResponse sendSmsResponse = smsUtil.sendSms(smsCodeQuery.getPhone(), smsCodeQuery.getCode());
        // 发送验证码成功
        // 获取GSON元素对象
        String bizId = GsonUtils.fromJavaObject(sendSmsResponse)
                                .getAsJsonObject().getAsJsonObject("body")
                                .getAsJsonObject().get("bizId").getAsString();
        SendStatisticsQueryDTO queryDTO = new SendStatisticsQueryDTO();
        queryDTO.setPhoneNumber(smsCodeQuery.getPhone())
                .setBizId(bizId)
                .setCurrentPage(1L)
                .setPageSize(1L)
                .setSendDate(DateUtil.getShortNowString());
        // 获取发送短信详情信息
        QuerySendDetailsResponse querySendDetails = smsUtil.querySendDetails(queryDTO);
        String content = GsonUtils.fromJavaObject(querySendDetails)
                .getAsJsonObject().getAsJsonObject("body")
                .getAsJsonObject().getAsJsonObject("smsSendDetailDTOs")
                .getAsJsonArray("smsSendDetailDTO")
                .get(0).getAsJsonObject().get("content").getAsString();

        // 获取短信内容 解析短信内容 提取验证码数据
        String codeFromContent = smsUtil.extractCodeFromContent(content);
        boolean validSmsCodeResult = smsUtil.validSmsCode(smsCodeQuery.getCode(), codeFromContent);
        // 校验通过，校验验证码存储redis key: sms:code:phone  value: codeFromContent
        if (validSmsCodeResult) {
            String key = SMS_CODE_PREFIX+ smsCodeQuery.getPhone();
            redisCache.set(key,codeFromContent,aliyunSmsProperties.getExpireTime(), TimeUnit.SECONDS);
        }
    }
}
